package com.ruibei.utils;

import com.ruibei.App;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.view.Display;
import android.view.WindowManager;

public class BitmapUtil {
	/**
	 * 
	 * @param bmp 需要修改的图片
	 * @param scale 设置缩放的比例
	 * @param scaleWidth 
	 * @param scaleHeight
	 * @return
	 */
	public static Bitmap calculateInSampleSize(Bitmap bmp,double scale,float scaleWidth,float scaleHeight) {
		
		int bmpWidth=bmp.getWidth(); 
        int bmpHeight=bmp.getHeight();
         
        /* 计算出这次要缩小的比例 */ 
        scaleWidth=(float) (scaleWidth*scale); 
        scaleHeight=(float) (scaleHeight*scale); 
        /* 产生reSize后的Bitmap对象 */
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        Bitmap resizeBmp = Bitmap.createBitmap(bmp,0,0,bmpWidth, 
                bmpHeight,matrix,true); 

		return resizeBmp;
	}
	/**
	 * 显示图片时，先将图片进行处理，避免图片过大，出现内存溢出
	 * @param filePath
	 * @return
	 */
	public static Bitmap getScaleBitmap(String filePath) {
		BitmapFactory.Options opt = new BitmapFactory.Options();
		opt.inJustDecodeBounds = true;
		Bitmap bmp = BitmapFactory.decodeFile(filePath, opt);

		int bmpWidth = opt.outWidth;
		int bmpHeght = opt.outHeight;

		WindowManager windowManager = (WindowManager) App.getContext().getSystemService(Context.WINDOW_SERVICE);
		Display display = windowManager.getDefaultDisplay();
		int screenWidth = display.getWidth();
		int screenHeight = display.getHeight();

		opt.inSampleSize = 1;
		if (bmpWidth > bmpHeght) {
			if (bmpWidth > screenWidth)
				opt.inSampleSize = bmpWidth / screenWidth;
		} else {
			if (bmpHeght > screenHeight)
				opt.inSampleSize = bmpHeght / screenHeight;
		}
		opt.inJustDecodeBounds = false;

		bmp = BitmapFactory.decodeFile(filePath, opt);
		return bmp;
		}
}
